using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Ventigrate.Shared.ExternalMembership.Internal;
using SWS = System.Web.Security;

namespace Ventigrate.Shared.ExternalMembership.WebControls
{
    /// <summary>
    /// Code behind for UserNew.aspx
    /// </summary>
    public class UserNew : LayoutsPageBase
    {
        protected InputFormSection RolesSection;
        protected InputFormCheckBoxList rolesList;
        protected InputFormCheckBox isActive;
        protected InputFormCheckBox emailUser;
        protected InputFormTextBox txtEmail;
        protected InputFormTextBox txtDescription;
        protected InputFormTextBox txtPassword;
        protected InputFormTextBox txtUsername;
        protected Label lblMessage;

        protected override bool RequireSiteAdministrator
        {
            get { return true; }
        }

        private ExtranetContext _ctx = null;
        internal ExtranetContext Ctx
        {
            get
            {
                if (null == _ctx)
                    _ctx = ExtranetContext.Default;

                return _ctx;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            // init
            this.CheckRights();

            if (!Page.IsPostBack)
            {
                try
                {
                    // if roles activated display roles
                    if (Ctx.RoleProvider != null)
                    {
                        RolesSection.Visible = true;

                        // load roles
                        rolesList.DataSource = Roles.GetAllRoles();
                        rolesList.DataBind();
                    }
                }
                catch (Exception ex)
                {
                    ULS.Log.Error(ex);
                    SPUtility.TransferToErrorPage(ex.Message); 
                }
            }
        }

        protected void OnSubmit(object sender, EventArgs e)
        {
            // check to see if username already in use
            SWS.MembershipUser user = Membership.GetUser(txtUsername.Text);

            if (user == null)
            {
                try
                {
                    // create FBA database user
                    user = Membership.CreateUser(txtUsername.Text, txtPassword.Text, txtEmail.Text);
                    user.Comment = txtDescription.Text;
                    user.IsApproved = isActive.Checked;
                    Membership.UpdateUser(user);

                    if (Ctx.RoleProvider != null)
                    {
                        for (int i = 0; i < rolesList.Items.Count; i++)
                        {
                            if (rolesList.Items[i].Selected)
                            {
                                Roles.AddUserToRole(user.UserName, rolesList.Items[i].Value);
                            }
                        }
                    }

                    // redirect back to display page
                    Response.Redirect("UsersDisp.aspx", false);
                }
                catch (Exception ex)
                {
                    ULS.Log.Error(ex);
                    SPUtility.TransferToErrorPage(ex.Message); 
                }
            }
            else
            {
                lblMessage.Text = "User Already Exists";
            }
        }
    }
}
